package com.bjsxt.service.imp;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bjsxt.commons.DataGridModel;
import com.bjsxt.mapper.DrugMapper;
import com.bjsxt.mapper.SellMapper;
import com.bjsxt.mapper.StaffMapper;
import com.bjsxt.pojo.Drug;
import com.bjsxt.pojo.DrugExample;
import com.bjsxt.pojo.DrugExample.Criteria;
import com.bjsxt.pojo.Sell;
import com.bjsxt.pojo.SellExample;
import com.bjsxt.service.SellMedicineService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 销售药品业务接口实现
 * 
 * @author 程应康
 *
 */
@Service
public class SellMedicineServiceImpl implements SellMedicineService {
	@Autowired
	private DrugMapper drugMapper;
	
	@Autowired
	private SellMapper sellMapper;
	
	@Autowired
	private StaffMapper staffMapper;

	@Override
	public DataGridModel findDrug(Drug drug, Integer page, Integer rows){
		PageHelper.startPage(page, rows);
		DrugExample de = new DrugExample();
		List<Drug> drugs = null;

		Criteria criteria = de.createCriteria();
		if (drug.getDrugid() != null && drug.getDrugid() > 0) {
			criteria.andDrugidEqualTo(drug.getDrugid());
		}
		if (drug.getDrugname() != null && drug.getDrugname().length() > 0) {
			criteria.andDrugnameLike("%" + drug.getDrugname() + "%");
		}
		if (drug.getDrugsal() != null && drug.getDrugsal() > 0) {
			criteria.andDrugsalEqualTo(drug.getDrugsal());
		}
		if (drug.getDrugeffect() != null && drug.getDrugeffect().length() > 0) {
			criteria.andDrugeffectLike("%" + drug.getDrugeffect() + "%");
		}
		criteria.andDrugplanEqualTo(drug.getDrugplan());
		drugs = drugMapper.selectByExample(de);
		PageInfo<Drug> pd = new PageInfo<>(drugs);
		long total = pd.getTotal();

		DataGridModel dgm = new DataGridModel();
		dgm.setRows(drugs);
		dgm.setTotal(total);
		return dgm;
	}

	@Override
	public DataGridModel findDrug(Integer page, Integer rows) {
		PageHelper.startPage(page, rows);
		DrugExample de = new DrugExample();
		List<Drug> drugs = drugMapper.selectByExample(de);
		PageInfo<Drug> pd = new PageInfo<>(drugs);
		long total = pd.getTotal();

		DataGridModel dgm = new DataGridModel();
		dgm.setRows(drugs);
		dgm.setTotal(total);
		return dgm;
	}

	@Override
	public void addsallDrug(String drugids, String drugnames, String count,Integer staffid) throws Exception {
		String[] idArr = drugids.split(",");
		String[] nameArr = drugnames.split(",");
		String[] countArr = count.split(",");
		//获取当前时间
		Date d = new Date();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String date = sdf.format(d);
		//循环插入数据
		for (int i = 0; i < countArr.length; i++) {
			Sell sell = new Sell();
			sell.setStaffid(staffid);
			sell.setDrugid(Integer.parseInt(idArr[i]));
			sell.setSelldate(date);
			sell.setSellcount(Integer.parseInt(countArr[i]));
			sellMapper.insertSelective(sell);
		}
	}

	@Override
	public DataGridModel findsaleLog(Integer page, Integer rows) {
		PageHelper.startPage(page, rows);
		SellExample se = new SellExample();
		//获取sell表的内容
		List<Sell> sells = sellMapper.selectByExample(se);
		PageInfo<Sell> pis = new PageInfo<>(sells);
		long total = pis.getTotal();
		
		for (int i = 0; i < sells.size(); i++) {
			Sell sell = sells.get(i);
			String staffname = staffMapper.selectByPrimaryKey(sell.getStaffid()).getStaffname();
			String drupname = drugMapper.selectByPrimaryKey(sell.getDrugid()).getDrugname();
			sell.setDrugname(drupname);
			sell.setStaffname(staffname);
		}
		
		DataGridModel dgm = new DataGridModel();
		dgm.setRows(sells);
		dgm.setTotal(total);
		
		return dgm;
	}

	@Override
	public DataGridModel findsaleLog(Integer page, Integer rows, Sell sell) {
		PageHelper.startPage(page, rows);
		SellExample se = new SellExample();
		List<Sell> sells = null;
		com.bjsxt.pojo.SellExample.Criteria criteria = se.createCriteria();
		if (sell.getStaffid()!=null && sell.getStaffid() > 0) {
			criteria.andStaffidEqualTo(sell.getStaffid());
		}
		if (sell.getDrugid()!=null && sell.getDrugid() > 0) {
			criteria.andDrugidEqualTo(sell.getDrugid());
		}
		if (sell.getSelldate()!=null && sell.getSelldate().length() > 0) {
			criteria.andSelldateLike("%"+sell.getSelldate()+"%");
		}
		sells = sellMapper.selectByExample(se);
		
		for (int i = 0; i < sells.size(); i++) {
			Sell sl = sells.get(i);
			sl.setDrugname(drugMapper.selectByPrimaryKey(sl.getDrugid()).getDrugname());
			sl.setStaffname(staffMapper.selectByPrimaryKey(sl.getStaffid()).getStaffname());
		}
		
		PageInfo<Sell> pis = new PageInfo<>(sells);
		long total = pis.getTotal();
		
		DataGridModel dgm = new DataGridModel();
		dgm.setRows(sells);
		dgm.setTotal(total);
		return dgm;
	}
}
